<?php

namespace App\Models\Article;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

use App\Models\Article\Comment;

class Article extends Model
{
    use HasFactory;

    // 约定表名
    protected $table = "article";

    //关联查询方法
    public function type()
    {
        return $this->belongsTo('App\Models\Article\Type', 'typeid', 'id')->withDefault();
    }

    public function getCommentAttribute()
    {
        //获取主键id
        $artid = $this->id;

        $comment = Comment::where(['artid' => $artid])->count();

        return $comment;
    }
}
